package com.mocha.bass.test;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.hyzh.latte.NezhaApplication;
import com.hyzh.latte.vehicle.dmo.PointBO;
import com.hyzh.latte.vehicle.dmo.entity.PointPGEntity;
import com.hyzh.latte.vehicle.dmo.entity.PolePGEntity;
import com.hyzh.latte.vehicle.dmo.entity.PolePointPGEntity;
import com.hyzh.latte.vehicle.repository.CameraPGRepository;
import com.hyzh.latte.vehicle.repository.PointPGRepository;
import com.hyzh.latte.vehicle.repository.PolePGRepository;
import com.hyzh.latte.vehicle.repository.PolePointPGRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author songkui
 * @since 2024/5/8 15:03
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = NezhaApplication.class)
@Slf4j
public class JianganPolePoint {
    private final static String PARK = "jiangan";

    @Autowired
    private PointPGRepository pointPGRepository;
    @Autowired
    private PolePointPGRepository polePointPGRepository;
    @Autowired
    private CameraPGRepository cameraPGRepository;
    @Autowired
    private PolePGRepository polePGRepository;

    @Test
    public void polePoint(){
        List<PointPGEntity> pointList = pointPGRepository.lambdaQuery().eq(PointPGEntity::getParkCode, PARK).list();
        Map<String, PointPGEntity> pom = pointList.stream().collect(Collectors.toMap(PointPGEntity::getPointNo, Function.identity()));

        List<PolePGEntity> poleList = polePGRepository.lambdaQuery().eq(PolePGEntity::getParkCode, PARK).list();
        poleList = poleList.stream().filter(p -> StrUtil.isNotBlank(p.getXNo()) && StrUtil.isNotBlank(p.getYNo())).collect(Collectors.toList());

        for (PolePGEntity pgEntity : poleList){
            doPolePoint(pgEntity.getPoleCode(), "x", pgEntity.getPoleCode(), "y", pgEntity.getXNo()+","+pgEntity.getYNo(), pom);
            doPolePoint(pgEntity.getPoleCode(), "y", pgEntity.getPoleCode(), "x", pgEntity.getYNo()+","+pgEntity.getXNo(), pom);
        }


    }

    public void doPolePoint(String sr, String srL,  String  tg, String tgL, String points, Map<String, PointPGEntity> pom){

        PolePointPGEntity pgEntity = new PolePointPGEntity();
        pgEntity.setSource(sr);
        pgEntity.setSourceLab(srL);
        pgEntity.setTarget(tg);
        pgEntity.setTargetLab(tgL);
        pgEntity.setDistance(50);
        pgEntity.setDirection(-1);


        String json = pointJSON(pom, points);
        pgEntity.setPoints(points);
        pgEntity.setPointJson(json);
        pgEntity.setParkCode(PARK);

        polePointPGRepository.save(pgEntity);

    }

    public static String pointJSON(Map<String, PointPGEntity> pom, String points){
        String[] ss = points.split(",");
        List<PointBO> pointBOList = new ArrayList<>(ss.length);
        for (String s : ss){
            PointPGEntity entity = pom.get(s);
            PointBO pointBO = new PointBO();
            pointBO.setLon(entity.getLon());
            pointBO.setLat(entity.getLat());
            pointBO.setH(entity.getHeight());
            pointBO.setT(1);
            pointBO.setDistance(50);
            pointBOList.add(pointBO);
        }


        return JSON.toJSONString(pointBOList);
    }


}
